//
//  SubjectsViewController.swift
//  Example
//
//  Created by T AO on 2022/2/11.
//

import UIKit
import RxSwift

// MARK: - subjects 即使订阅者也是observable

class SubjectsViewController: BaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

//        publishSubject()
//        behaviorSubject()
        replaySubject()
    }

}

extension SubjectsViewController {
    /// PublishSubject
    /// 不需要初始值就可以创建
    private func publishSubject() {
        let subject = PublishSubject<String>()
        
        subject.onNext("0000")
        
        subject.subscribe(onNext: { string in
            print("第一次订阅", string)
        }, onError: { error in
            print(error)
        }, onCompleted: {
            print("第一次 completed")
        }).disposed(by: disposeBag)
        
        subject.onNext("1111")
        
        subject.subscribe { string in
            print("第二次订阅", string)
        } onError: { error in
            print(error)
        } onCompleted: {
            print("第二次 completed")
        }.disposed(by: disposeBag)

        subject.onNext("2222")
        
        subject.onCompleted()
        
        subject.onNext("3333")
        
        subject.subscribe { string in
            print("第三次订阅", string)
        } onError: { error in
            print(error)
        } onCompleted: {
            print("第三次 completed")
        }.disposed(by: disposeBag)
        
        /**
         打印：
         第一次订阅 1111
         第一次订阅 2222
         第二次订阅 2222
         第一次 completed
         第二次 completed
         第三次 completed
         */
    }
    
    /// BehaviorSubject
    /// 需要一个默认值来创建
    private func behaviorSubject() {
        let subject = BehaviorSubject(value: 111)
        
        subject.subscribe { event in
            print("第一次订阅", event)
        }.disposed(by: disposeBag)
        
        subject.onNext(222)
        
        subject.onError(NSError(domain: "local", code: 0, userInfo: nil))
        
        subject.subscribe { event in
            print("第二次订阅", event)
        }.disposed(by: disposeBag)
        
        /**
         打印：
         第一次订阅 next(111)
         第一次订阅 next(222)
         第一次订阅 error(Error Domain=local Code=0 "(null)")
         第二次订阅 error(Error Domain=local Code=0 "(null)")
         */
    }
    
    /// ReplaySubject bufferSize
    private func replaySubject() {
        let subject = ReplaySubject<String>.create(bufferSize: 2)
        
        subject.onNext("111")
        subject.onNext("222")
        subject.onNext("333")
        
        subject.subscribe { event in
            print("第一次订阅", event)
        }.disposed(by: disposeBag)
        
        subject.onNext("444")
        
        subject.subscribe { event in
            print("第二次订阅", event)
        }.disposed(by: disposeBag)
        
        subject.onCompleted()
        
        subject.subscribe { event in
            print("第三次订阅", event)
        }.disposed(by: disposeBag)
        
        /**
         打印：
         第一次订阅 next(222)
         第一次订阅 next(333)
         第一次订阅 next(444)
         第二次订阅 next(333)
         第二次订阅 next(444)
         第一次订阅 completed
         第二次订阅 completed
         第三次订阅 next(333)
         第三次订阅 next(444)
         第三次订阅 completed
         */
    }
}
